﻿using CoreFtp;
using CoreFtp.Enum;
using System;
using System.IO;
using Quartz.Util;

namespace FtpUploadWeb.Common
{
    /// <summary>
    /// FTP帮助类
    /// </summary>
    public class FtpHelper
    {
        #region 字段
        /// <summary>
        /// FTP服务器
        /// </summary>
        public static string host;
        /// <summary>
        /// 账号
        /// </summary>
        public static string user;
        /// <summary>
        /// 密码
        /// </summary>
        public static string password;
        /// <summary>
        /// 端口号
        /// </summary>
        public static int port;
        /// <summary>
        /// 本地上传文件目录
        /// </summary>
        public static string upLoadFileDirectory;//
        //public static string ftpFileDirectory;//上传到ftp文件
        /// <summary>
        /// 上传到ftp服务器目录 
        /// </summary>
        public static string ftpSaveDirectory;//上传到ftp目录 
      
        #endregion

        /// <summary>
        /// 连接Ftp服务器
        /// </summary>
        public async static void ConnectionFtp()
        {
            using (var ftpClient = new FtpClient(new FtpClientConfiguration
            {
                Host = host,
                Username = user,
                Password = password,
                Port = port,
                EncryptionType = FtpEncryption.Implicit,
                IgnoreCertificateErrors = true
            }))
            {
                await ftpClient.LoginAsync();
            }
        }

        /// <summary>
        /// 上传文件到服务器
        /// </summary>
        public async static void Upload()
        {
            var filePath = ZipFileHelper.getZipFilePath();
            var fileInfo = new FileInfo(filePath);
            if (!fileInfo.Exists)
            {
                TaskManage.WriteTextLog("Upload", "需要上传的压缩包不存在");
                return;
            }

            try
            {
                using (var ftpClient = new FtpClient(new FtpClientConfiguration
                {
                    Host = host,
                    Username = user,
                    Password = password
                }))
                {
                    await ftpClient.LoginAsync();

                    var ftpFilePath = fileInfo.Name;
                    if (!ftpSaveDirectory.IsNullOrWhiteSpace())
                    {
                        //创建目录
                        await ftpClient.CreateDirectoryAsync(ftpSaveDirectory);
                        ftpFilePath = ftpSaveDirectory + "//" + fileInfo.Name;
                    }

                    using (var writeStream =
                        await ftpClient.OpenFileWriteStreamAsync(ftpFilePath))
                    {
                        var fileReadStream = fileInfo.OpenRead();
                        await fileReadStream.CopyToAsync(writeStream);
                        fileReadStream.Close();
                        var msg = "上传成功: " + filePath +
                                  "  保存到ftp目录: " + FtpHelper.ftpSaveDirectory;
                        TaskManage.WriteTextLog("Upload", msg);
                    }
                }
            }
            catch (Exception ex)
            {
                TaskManage.WriteTextLog("Upload", "异常:" + ex.ToString());
            }
        }
    }
}
